//
//  UITableViewCell+Addition.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/1.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "UITableViewCell+Addition.h"

@implementation UITableViewCell (Addition)

+ (instancetype)cellWithTableView:(UITableView *)tableView forIndexPath:(NSIndexPath *)indexpath
{
    if (!tableView) {
        return [[self alloc] init];
    }
    NSString *classStr = [NSStringFromClass([self class]) stringByAppendingString:@"cellID"];
    [tableView registerClass:[self class] forCellReuseIdentifier:classStr];
    return [tableView dequeueReusableCellWithIdentifier:classStr forIndexPath:indexpath];
}
+ (instancetype)nibCellWithTableView:(UITableView *)tableView forIndexPath:(NSIndexPath *)indexpath{
    if (tableView == nil) {
        return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
    }
    NSString *classname = NSStringFromClass([self class]);
    NSString *identifier = [classname stringByAppendingString:@"nibCellID"];
    UINib *nib = [UINib nibWithNibName:classname bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:identifier];
    return [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexpath];
}

@end


@implementation UICollectionViewCell(Addition)

+ (instancetype)cellWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexpath
{
    if (!collectionView) {
        return [[self alloc] init];
    }
    NSString *classStr = [NSStringFromClass([self class]) stringByAppendingString:@"cellID"];
    [collectionView registerClass:[self class] forCellWithReuseIdentifier:classStr];
    return [collectionView dequeueReusableCellWithReuseIdentifier:classStr forIndexPath:indexpath];
}
+ (instancetype)nibCellWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexpath{
    if (collectionView == nil) {
        return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
    }
    NSString *classname = NSStringFromClass([self class]);
    NSString *identifier = [classname stringByAppendingString:@"nibCellID"];
    UINib *nib = [UINib nibWithNibName:classname bundle:nil];
    [collectionView registerNib:nib forCellWithReuseIdentifier:identifier];
    return [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexpath];
}

@end
